package com.itheima.Collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo01 {
    public static void main(String[] args) {

        ArrayList<Integer> list1=new ArrayList<>();
//默认规则cort
        Collections.addAll(list1,10,1,3,4,2,6,4,7);
        //Collections.sort(list1);
       // System.out.println(list1);


//自己指定规则sort
      Collections.sort(list1, new Comparator<Integer>() {
          @Override
          public int compare(Integer o1, Integer o2) {
              return 02 - 01;
          }
      });

      //  System.out.println(list1);
        Collections.sort(list1,(o1,o2)->o2-o1);
       // System.out.println(list1);//[10, 7, 6, 4, 4, 3, 2, 1]

       //一二分查找查找元素
        ArrayList<Integer> list2=new ArrayList<>();
        Collections.addAll(list2,1,2,3,4,5,6,7,8,9,10);
       // System.out.println(Collections.binarySearch(list2,0));//-1
       // System.out.println(Collections.binarySearch(list2,1));//0
       // System.out.println(Collections.binarySearch(list2,20));//-11

        ArrayList<Integer> list3=new ArrayList<>();
        ArrayList<Integer> list4=new ArrayList<>();

        Collections.addAll(list3,1,2,3,4,5,6,7,8,9,10);
        Collections.addAll(list4,0,0,0,0,0,0,0,0,0,0,0,0,0);
        //拷贝数组数组中的元素
        Collections.copy(list4,list3);
       // System.out.println(list3);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
       // System.out.println(list4);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0]

        ArrayList<Integer> list5 = new ArrayList<>();
        Collections.addAll(list5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Collections.fill(list5, 100);
       // System.out.println(list5);

        //求最大值最小值
        System.out.println(Collections.max(list5));//100

        //对于max,min指定规则
        ArrayList<String> list7=new ArrayList<>();
        Collections.addAll(list7,"a","aa","aaa","aaaa");
        Collections.max(list7, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {

                return o1.length()-o2.length();
            }
        });
        System.out.println(Collections.max(list7));

        //实现交换元素
        System.out.println("--------");
        ArrayList<Integer> list8=new ArrayList<>();
        Collections.addAll(list8,1,2,3);
        Collections.swap(list8,0,2);
        System.out.println(list8);//[3, 2, 1]
    }

    private static void method1() {
        ArrayList<String> list =new ArrayList<>();
        Collections.addAll(list,"abc","bcd","qwer","df","asdf","zxcv","1234","qwer");

        System.out.println(list);

        Collections.shuffle(list);
        System.out.println(list);
    }
}
